/**
 * 
 */
package com.nwpusp.model.players;

import java.io.Serializable;

import com.nwpusp.model.characters.*;
import com.nwpusp.model.session.ConnectionId;
import com.nwpusp.model.session.MessageSession;

/**
 * @author JustinCao
 *
 */
public class Player {

	public enum PlayerStatus {initial, ready, waiting, waitingAndResponse/*only for server konw*/, playing, reachEnd, over}
	private ConnectionId cid = null;
	private String name = null;
	private GameRecord record = null;
	private GameCharacter ch = null;
	private static ConnectionId localCid = null;
	private PlayerStatus sts = PlayerStatus.initial;
	
	public static void setLocalCid(ConnectionId cid) {
		localCid = new ConnectionId(cid.getCid());
	}
	
	@Override
	public boolean equals(Object target) {
		if(target != null && target instanceof Player) {
			return cid.equals(((Player)target).getConnectionId());
		}
		return false;
	}
// getters & setters {
	public PlayerStatus getStatus() {
		return sts;	
	}
	
	public String getName() {
		return name;
	}

	public GameRecord getRecord() {
		return record;
	}

	public GameCharacter getCh() {
		return ch;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setCh(GameCharacter ch) {
		this.ch = ch;
		if(ch != null) {
			ch.setName(this.name);
		}
	}
	
	public void setRecord(GameRecord record) {
		this.record = record;
	}
	
	public void setStatus(PlayerStatus sts) {
		this.sts = sts;
		if(sts == PlayerStatus.waiting || sts == PlayerStatus.waitingAndResponse) {
			getCh().setStatus(GameCharacter.CHARACTERSTS_WaitingToStart);
		}
		else if(sts == PlayerStatus.playing) {
			getCh().setStatus(GameCharacter.CHARACTERSTS_Running);
		}
	}
	
	public boolean isReady() {
		return sts == PlayerStatus.ready;
	}
// } getters & setters
	
	public boolean isLocalPlayer() {
		if(cid != null && localCid != null) {
			return (cid.equals(localCid));
		}
		return false;
	}
	
	public ConnectionId getConnectionId() {
		return cid;
	}
	
	public Player(ConnectionId cid, String name, GameCharacter ch, GameRecord record) {
		this.cid = new ConnectionId(cid.getCid());
		this.name = name;
		setCh(ch);
		this.record = record;
	}

	public void reachEnd() {
		setStatus(PlayerStatus.reachEnd);
	}
}
